#include <project.h>
#include "userMain.h"
#include "userLightDriver.h"

#define     NIGHT_LEVEL_HIGH     200
#define     NIGHT_LEVEL_LOW      30

unsigned char userNightValueNow=0;
unsigned char userNightLevel = 1;
unsigned char userNightState = 0;
unsigned char userNightEnable = 0;

void userNightInitFunc(void)
{
    PWM_Night_Start();
    userNightLevelSetFunc(userNightLevel);
    userNightOff();
    //userNightOn();
}

void userNightOn(void)
{
   userNightModify(userNightValueNow);
   userNightState = 1;
}
void userNightOff(void)
{
    userNightModify(0x00);
	userNightState = 0;
}
void userNightModify(unsigned char nightValue)
{
    unsigned int userValue;
    userValue=nightValue*255;
    PWM_Night_Start();
    CyDelay(1);
    PWM_Night_WriteCompare(userValue);
}

void userNightLevelSetFunc(unsigned char nightLevel)
{
    if(nightLevel)
    {
        userNightValueNow=NIGHT_LEVEL_HIGH;
		userNightLevel = 1;
    }
    else
    {
        userNightValueNow=NIGHT_LEVEL_LOW;
		userNightLevel = 0;
    }
}

void userLightEnableSetFunc(unsigned char nightEnable)
{
	if(nightEnable)
	{
		userNightEnable = 1;
	}
	else
	{
		userNightEnable = 0;
	}
}

extern unsigned char userNightOperateState;
void userNightCtrlFunc(unsigned char ctrlMode,unsigned char ctrlValue)
{
    if(ctrlMode==0)
    {
        if(ctrlValue)
        {
            userNightOn();
            userNightOperateState = 0x01;
        }
        else
        {
            userNightOff();  
            userNightOperateState = 0x00;
        }
    }
    if(ctrlMode==1)
    {
        userNightLevelSetFunc(ctrlValue);
        if(userNightState)
        {
            userNightOn();
        }
    }
}

extern uint8 userBeepEnable;
extern uint8 userMotionEnable;
extern uint8 userLightSensorEnable;
void userEnableInfoCtrlFunc(unsigned char ctrlMode,unsigned char ctrlValue)
{
    switch(ctrlMode)
    {
        case 0x03:
        {
            userBeepEnableFunc(ctrlValue);
            break;
        }
        case 0x07:
        {
            userMotionSensorEnableFunc(ctrlValue);
            break;
        }
        case 0x08:
        {
            userLightSensorEnableFunc(ctrlValue);
            break;
        }
        default:
        {
            break;
        }
    }
}

